﻿using SHZY.Common.Core.Manager;
using SHZY.Common.Core.Security;
using SHZY.Common.Enum;
using SHZY.Common.Extension;
using SHZY.Common.Filter;
using SHZY.Common.Models;
using SHZY.Common.Security;
using SHZY.DependencyInjection;
using SHZY.DynamicApiController;
using SHZY.FriendlyException;
using SHZY.Systems.Entitys.Permission;
using SHZY.Systems.Entitys.System;
using SHZY.Systems.Interfaces.System;
using SHZY.system.Entitys.Dto.QmsBaseIpqccalendar;
using SHZY.system.Entitys;
using SHZY.system.Interfaces;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace SHZY.system;

/// <summary>
/// 业务实现：日程安排.
/// </summary>
[ApiDescriptionSettings(Tag = "system", Name = "QmsBaseIpqccalendar", Order = 200)]
[Route("api/system/[controller]")]
public class QmsBaseIpqccalendarService : IQmsBaseIpqccalendarService, IDynamicApiController, ITransient
{
    /// <summary>
    /// 服务基础仓储.
    /// </summary>
    private readonly ISqlSugarRepository<QmsBaseIpqccalendarEntity> _repository;

    /// <summary>
    /// 数据接口服务.
    /// </summary>
    private readonly IDataInterfaceService _dataInterfaceService;

    /// <summary>
    /// 用户管理.
    /// </summary>
    private readonly IUserManager _userManager;

    /// <summary>
    /// 初始化一个<see cref="QmsBaseIpqccalendarService"/>类型的新实例.
    /// </summary>
    public QmsBaseIpqccalendarService(
        ISqlSugarRepository<QmsBaseIpqccalendarEntity> qmsBaseIpqccalendarRepository,
        IDataInterfaceService dataInterfaceService,
        ISqlSugarClient context,
        IUserManager userManager)
    {
        _repository = qmsBaseIpqccalendarRepository;
        _dataInterfaceService = dataInterfaceService;
        _userManager = userManager;
    }

    /// <summary>
    /// 获取日程安排.
    /// </summary>
    /// <param name="id">主键值.</param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<dynamic> GetInfo(string id)
    {
        return (await _repository.FirstOrDefaultAsync(x => x.Id == id)).Adapt<QmsBaseIpqccalendarInfoOutput>();
    }

    /// <summary>
    /// 获取日程安排列表.
    /// </summary>
    /// <param name="input">请求参数.</param>
    /// <returns></returns>
    [HttpGet("")]
    public async Task<dynamic> GetList([FromQuery] QmsBaseIpqccalendarListQueryInput input)
    {
        var data = await _repository.Context.Queryable<QmsBaseIpqccalendarEntity>()
            .WhereIF(!string.IsNullOrEmpty(input.year), it => it.Year.Contains(input.year))
            .WhereIF(!string.IsNullOrEmpty(input.keyword), it => it.Year.Contains(input.keyword))
            .OrderByIF(string.IsNullOrEmpty(input.sidx), it => it.Id)
            .Select(it => new QmsBaseIpqccalendarListOutput
            {
                id = it.Id,
                year = it.Year,
                enabledMark = SqlFunc.IIF(it.EnabledMark == 0, "关", "开"),
            }).OrderByIF(!string.IsNullOrEmpty(input.sidx), it => input.sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
        return PageResult<QmsBaseIpqccalendarListOutput>.SqlSugarPageResult(data);
    }

    /// <summary>
    /// 新建日程安排.
    /// </summary>
    /// <param name="input">参数.</param>
    /// <returns></returns>
    [HttpPost("")]
    public async Task Create([FromBody] QmsBaseIpqccalendarCrInput input)
    {
        var entity = input.Adapt<QmsBaseIpqccalendarEntity>();
        entity.Id = SnowflakeIdHelper.NextId();
        var isOk = await _repository.Context.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
        if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1000);
    }

    /// <summary>
    /// 更新日程安排.
    /// </summary>
    /// <param name="id">主键值.</param>
    /// <param name="input">参数.</param>
    /// <returns></returns>
    [HttpPut("{id}")]
    public async Task Update(string id, [FromBody] QmsBaseIpqccalendarUpInput input)
    {
        var entity = input.Adapt<QmsBaseIpqccalendarEntity>();
        var isOk = await _repository.Context.Updateable(entity).UpdateColumns(it => new 
        {
            it.Year,
            it.EnabledMark,
        }).ExecuteCommandAsync();
        if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1001);
    }

    /// <summary>
    /// 删除日程安排.
    /// </summary>
    /// <returns></returns>
    [HttpDelete("{id}")]
    public async Task Delete(string id)
    {
        var isOk = await _repository.Context.Deleteable<QmsBaseIpqccalendarEntity>().Where(it => it.Id.Equals(id)).ExecuteCommandAsync();
        if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1002);
    }
}